package leetcode.t1Array;

import java.util.HashSet;

/**
 * @author K
 * @date 2024/1/23 13:40
 */
public class T26_RemoveDuplicates {


    /**
     * 26. 删除有序数组中的重复项
     *
     * @param nums
     * @return
     */
    public static int removeDuplicates(int[] nums) {
        //记录不重复的下标位置
        int left = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[left] == nums[i]) {
                continue;
            }
            nums[++left] = nums[i];
        }

        //返回不重复元素个数
        return left + 1;
    }

    public int removeDuplicates2(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                continue;
            }
            set.add(nums[i]);
            nums[index] = nums[i];
            index++;
        }

        return set.size();
    }

    public static void main(String[] args) {
        int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        int[] nums2 = {1, 1, 2};
        int i = removeDuplicates(nums);
        System.out.println();
    }

}
